'use strict';

const Service = require('egg').Service;
const dayjs = require('dayjs');
class UserService extends Service {
  async login({ name, password }) {
    return await this.ctx.model.User.find({ name, password });
  }

  async find({ pageSize, pageNo, ...reset }) {
    const data = await this.ctx.model.User.find({ ...reset })
      .limit(pageSize)
      .skip((pageNo - 1) * pageSize);
    const total = await this.ctx.model.User.find({ ...reset }).countDocuments();
    return {
      data,
      total,
    };
  }
  async create({ name, password }) {
    return await this.ctx.model.User.create({
      name,
      password,
      createTime: dayjs().valueOf(),
    });
  }
  async deleteOne({ _id }) {
    return await this.ctx.model.User.deleteOne({
      _id,
    });
  }

  async update({ _id, ...reset }) {
    return await this.ctx.model.User.updateOne(
      {
        _id,
      },
      {
        ...reset,
      }
    );
  }
}

module.exports = UserService;
